// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var router = require("../hash/pageRouter");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var databind = require("databind");
    var firstVisited = 0;
    var tools = require("tools");
    var auditState;
    var leftM;
    var html_createSuppliers = require("./html_createSuppliers.html");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(){
    	router.init();
    	inquireSupplierInfoPageList();
		initEvent();
    };
    
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$(".memberGradeAduitList").unbind("click").click(function(){
			router.route("client/memberGradeaudit.html");
		});
    };
    
    //新增企业
    var addSupplierInfo = function() {
    	var ii = layer.open({
  		  title: '新增客户',
  		  type: 1,
  		  skin: 'layui-layer-rim', //加上边框
  		  area: ['800px', '350px'], //宽高
  		  content: html_createSuppliers,
  		  btn: ['新增', '取消'],
  		  yes: function(){
  		  	databind.submitDom($(".createSupplierInfo"), function(){
  		  		layer.msg("新增成功！");
  		  		layer.close(ii);
  		  		inquireSupplierInfoPageList();
  		  	});
  		  }
  		});
  		
  		new PCAS("registerAddressProvince","registerAddressCity","registerAddressDistrict");
  		new PCAS("workAddressProvince","workAddressCity","workAddressDistrict");
  		new PCAS("businessLicenceAddress","businessCity","businessDistrict");
    	
    };    
	
	//启用企业
	var enableSupplierInfo = function(supplierId) {
		base.fnAjax({
			action: 'enableSupplierInfo.json',
			async: false,
			oData: {
				supplierId: supplierId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "启用成功！");
					inquireSupplierInfoPageList();
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	// 禁用企业
	var disableSupplierInfo = function(supplierId) {
		base.fnAjax({
			action: 'disableSupplierInfo.json',
			async: false,
			oData: {
				supplierId: supplierId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "禁用成功！");
					inquireSupplierInfoPageList();
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	//编辑企业
	var editorSupplierInfo = function(supplierId) {
		//获取bean显示
		var ii;
		base.fnAjax({
			action: 'inquireSupplierInfoDetail.json',
			async: false,
			oData: {
				supplierId: supplierId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					
					base.alert(1, "获取成功！");
					ii = layer.open({
						  title: '编辑客户',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['800px', '350px'], //宽高
						  content: html_createSuppliers,
						  btn: ['编辑', '取消'],
						  yes: function(){
							  updateSupplierInfo(data.datas[0]);
							  layer.close(ii);
						  }
						});
					new PCAS("registerAddressProvince","registerAddressCity","registerAddressDistrict");
					new PCAS("workAddressProvince","workAddressCity","workAddressDistrict");
					new PCAS("businessLicenceAddress","businessCity","businessDistrict");
					databind.fillForm($(".createSupplierInfo"), data.datas[0]);
					$(".createSupplierInfo select[name=registerAddressCity]").html('<option value="'+data.datas[0].registerAddressCity+'">'+data.datas[0].registerAddressCity+'</option>');
					$(".createSupplierInfo select[name=registerAddressDistrict]").html('<option value="'+data.datas[0].registerAddressDistrict+'">'+data.datas[0].registerAddressDistrict+'</option>');
					$(".createSupplierInfo select[name=workAddressCity]").html('<option value="'+data.datas[0].workAddressCity+'">'+data.datas[0].workAddressCity+'</option>');
					$(".createSupplierInfo select[name=workAddressDistrict]").html('<option value="'+data.datas[0].workAddressDistrict+'">'+data.datas[0].workAddressDistrict+'</option>');
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	//更新客户
	var updateSupplierInfo = function(datas) {
		var supplier = base.parseForm($(".createSupplierInfo"));
		supplier["supplierId"] = datas.supplierId;
		supplier["enabled"] = datas.enabled;
		supplier["createDateTime"] = datas.createDateTime;
		base.fnAjax({
			action: 'updateSupplierInfo.json',
			async: false,
			oData: supplier,
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "编辑成功！");
					inquireSupplierInfoPageList();
				} else {
					base.alert(3, data.message);
				}
			}
		});
		
	};
	
    //获取客户列表
	var inquireSupplierInfoPageList = function(){
		
        base.fnAjax({
            action: "inquireSupplierInfoPageList.json",
            oData: {
            	pageIndex: pageIndex,
                pageSize: pageSize
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	juicer.register("formatCurrency", tools.formatCurrency);
                	
                	var tpl = $("#enterpriseListTpl").html();
                    var html = juicer(tpl, data);
                    $("#enterpriseListBody").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#clientPagenation"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireSupplierInfoPageList();
                    });
                    
                    //禁用企业
					$(".disable").unbind("click").click(function(){
						disableSupplierInfo($(this).parents("tr").attr("supplierId"));
					});
					
					//启用企业
					$(".enable").unbind("click").click(function(){
						enableSupplierInfo($(this).parents("tr").attr("supplierId"));
					});
					
					//编辑企业
					$(".editor").unbind("click").click(function(){
						editorSupplierInfo($(this).parents("tr").attr("supplierId"));
					});
					
					//新增企业
					$(".addSupplier").unbind("click").click(function(){
						addSupplierInfo($(this).parents("tr").attr("supplierId"));
					});
					
					tools.inquireCurrentOperationList($(".wrapper"));
					
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
    return {
        init:init
    };
});